<?php 
/**
 * 前台购物车页控制器
 */
class CartController extends CommonController{
	public function index(){
		// P($_SESSION['cart']);
		if (isset($_SESSION['cart']['goods'])) {
			// 商品数量
			$total_rows = $_SESSION['cart']['total_rows'];
			// 总价
			$total = $_SESSION['cart']['total'];
			foreach ($_SESSION['cart']['goods'] as $k => $v) {
				$g_info[$k] = $v;
				$g_info[$k]['color'] = $v['options']['color'];
				$g_info[$k]['size'] = $v['options']['size'];
			}
			// p($g_info);
			$this->assign('g_info',isset($g_info)?$g_info:array());
			$total = array(
				'total_rows'=>$_SESSION['cart']['total_rows'],
				'total'=>$_SESSION['cart']['total']
				);
			// p($total);
			$this->assign('total',isset($total)?$total:array());
		} 
		$this->display();
	}


	/**
	 * 删除购物车
	 */
	public function del_cart(){
		$g_id = Q('get.g_id');
		unset($_SESSION['cart']['goods'][$g_id]);
		$this->success('删除成功','index','0.5');
	}

	/**
	 * 清空购物车
	 */
	public function del_all(){
		Cart::delAll();
		$this->success('清空成功','index','0.5');
	}
	/**
	 * 异步删除选中商品
	 */
	public function del_sid(){
		if (!IS_AJAX) return;
		// p(Q('post.'));
		// p($_SESSION['cart']);
		$sid = Q('post.sid');
		$num = $_SESSION['cart']['goods'][$sid]['num'];
		$total = $_SESSION['cart']['goods'][$sid]['total'];
		unset($_SESSION['cart']['goods'][$sid]);
		$_SESSION['cart']['total_rows'] = $_SESSION['cart']['total_rows'] - $num;
		$_SESSION['cart']['total'] = $_SESSION['cart']['total'] - $total;

		// p($_SESSION['cart']);
		$this->ajax(array(
			'result'=> 1,
			'sid'=> $sid,
			'total_rows'=> $_SESSION['cart']['total_rows'],
			'total'=> $_SESSION['cart']['total'],
			'info' => '删除成功！'

			));
	}


	/**
	 * 异步增加数量价格
	 */
	public function price_url(){
		// p(Q('post.'));
		// 单价
		$price = Q('post.price');
		$price = isset($price) ? $price : '';
		// 数量
		$g_number = Q('post.this_input') + 1;
		// sid
		$sid = Q('post.sid');

		// p($_SESSION['cart']);
		$data=array(
			'sid'=>$sid,// 唯一 sid，添加购物车时自动生成
			'num'=>$g_number,
			'total'=>$price
		);
		Cart::update($data);
		// p($_SESSION['cart']);

		$this->ajax(array(
			// 每件商品总价
			'g_total' => $_SESSION['cart']['goods'][$sid]['total'],
			// 商品数量
			'total_rows' => $_SESSION['cart']['total_rows'],
			// 商品总价
			'total' => $_SESSION['cart']['total']
			)); 

	}

	/**
	 * 异步减少数量价格
	 */
	public function price_reduce(){
		// p(Q('post.'));
		// 单价
		$price = Q('post.price');
		$price = isset($price) ? $price : '';
		// 数量
		$g_number = Q('post.this_input') - 1;
		// sid
		$sid = Q('post.sid'); 
		$data=array(
			'sid'=>$sid,// 唯一 sid，添加购物车时自动生成
			'num'=>$g_number,
			'total'=>$price
		);
		Cart::update($data);
		$this->ajax(array(
			// 每件商品总价
			'g_total' => $_SESSION['cart']['goods'][$sid]['total'],
			// 商品数量
			'total_rows' => $_SESSION['cart']['total_rows'],
			// 商品总价
			'total' => $_SESSION['cart']['total']
			));


	}















	
}
















 ?>